package com.halflife.nettyio;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class EchoClient {
    EventLoopGroup group;
    ChannelFuture channelFuture;
    public static void main(String[] args) throws Exception {
        EchoClient client = new EchoClient();
        client.start();
        Scanner input = new Scanner(System.in);

        System.out.println("please input your name ");
        String name ;

        while(input.hasNext()){
            name = input.nextLine();
            System.out.println(name);
//            ByteBuf bf = Unpooled.copiedBuffer(name, StandardCharsets.UTF_8);
//            ByteBuf bf1 = Unpooled.copiedBuffer(name, StandardCharsets.UTF_8);
//            ByteBuf bf2 = Unpooled.copiedBuffer(name, StandardCharsets.UTF_8);
//            ByteBuf bf3 = Unpooled.copiedBuffer(name, StandardCharsets.UTF_8);
            NioSocketChannel nioSocketChannel = (NioSocketChannel) client.channelFuture.channel();
            System.out.println(nioSocketChannel.config().isTcpNoDelay());
            for (int i = 0; i < 200; i++) {
                ByteBuf bf = Unpooled.copiedBuffer(name, StandardCharsets.UTF_8);
                client.channelFuture.channel().writeAndFlush(bf).sync();
            }
//            client.channelFuture.channel().writeAndFlush(bf).sync();
//            client.channelFuture.channel().writeAndFlush(bf1).sync();
//            client.channelFuture.channel().writeAndFlush(bf2).sync();
//            client.channelFuture.channel().writeAndFlush(bf3).sync();
        }

    }

    public void start() throws Exception {
         group = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(group).channel(NioSocketChannel.class)
                .remoteAddress(new InetSocketAddress("192.168.31.129", 9090))
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new EchoClientHandler());
                    }
                });
        channelFuture = bootstrap.connect().sync();
        NioSocketChannel nioSocketChannel = (NioSocketChannel) channelFuture.channel();
        nioSocketChannel.config().setTcpNoDelay(true);
//        ByteBuf bf = ByteBufAllocator.DEFAULT.directBuffer();
//        String ab = "asfd";
//        for (int i = 0; i < 10000; i++) {
//            ab += "阿斯蒂芬";
//        }
//        channelFuture.channel().writeAndFlush(bf.writeCharSequence(ab, StandardCharsets.UTF_8));
//        channelFuture.channel().closeFuture().sync();
    }
}
